Page({
  data: {
    pageIndex: 1,
    loading: false,
    noMore: false,
    list: null
  },
  onLoad: function () {
    this.getCollect();
  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },
  onReachBottom: function () {
    if (this.data.noMore) return;
    this.setData({
      loading: true
    });
    this.getCollect();
  },
  getCollect: function () {
    wx.util.request('user.collect', {
      page: this.data.pageIndex
    }).then(collect => {
      this.setData({
        list: collect,
        loading: false,
        noMore: collect.length < 10,
        pageIndex: this.data.pageIndex + 1
      });
    });
  },
  browse: function () {
    wx.redirectTo({
      url: `../product/index`,
    });
  },
  delete: function (e) {
    let idx = e.currentTarget.dataset.idx;
    let list = this.data.list;
    wx.util.request('common.collect.del', {
      query: `id=${list[idx].id}`
    }).then(() => {
      wx.util.toast('已删除');
      list.splice(idx, 1);
      this.setData({
        list: list
      });
    });
  },
  detail: function (e) {
    wx.navigateTo({
      url: `../product/detail?id=${e.currentTarget.dataset.id}`,
    });
  },
  addCart: function (e) {
    wx.util.request('cart.new', {
      body: {
        sku_id: e.currentTarget.dataset.id,
        num: 1
      }
    }).then(() => {
      wx.util.toast('已放入购物车');
    });
  }
})